package com.chen.blue.security.handle;

import cn.hutool.json.JSONUtil;
import com.chen.blue.constant.HttpStatus;
import com.chen.blue.result.R;
import com.chen.blue.utils.ServletUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Serializable;

/**
 * Created By CLT On 2023/10/30.
 */
@SuppressWarnings("all")
@Slf4j
@Component("authenticationEntryPoint")
public class AuthorizationFailHandler implements AuthenticationEntryPoint, Serializable {
    /**
     * 认证失败处理类
     *
     * @param request
     * @param response
     * @param authException
     * @throws IOException
     * @throws ServletException
     */
    private static final long serialVersionUid = 1L;

    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {

        ServletUtil.renderString(JSONUtil.toJsonStr(R.faild(HttpStatus.C401.getCode(), HttpStatus.C401.getDesc())), response);
        return;

    }
}
